import AuthAPI from "@api/auth";
import UserAPI from "@api/user";
import {resetRouter} from "@themeDefault/router";
import {store} from "@themeDefault/store";

import {LoginData} from "@api/auth";
import {UserInfo} from "@api/user";
import {TOKEN_KEY} from "@themeDefault/enums/CacheEnum";

export const useUserStore = defineStore("user", () => {
	const user = ref<UserInfo>({
		roles: [],
		perms: [],
	});

	/**
	 * 登录
	 *
	 * @returns
	 * @param loginData
	 */
	function login(loginData: LoginData) {
		return new Promise<void>((resolve, reject) => {
			AuthAPI.login(loginData)
				.then((data) => {
					const {tokenType, accessToken} = data;
					localStorage.setItem(TOKEN_KEY, tokenType + " " + accessToken); // Bearer eyJhbGciOiJIUzI1NiJ9.xxx.xxx
					resolve();
				})
				.catch((error) => {
					reject(error);
				});
		});
	}

	// 获取信息(用户昵称、头像、角色集合、权限集合)
	function getUserInfo() {
		return new Promise<UserInfo>((resolve, reject) => {
			UserAPI.getInfo()
				.then((res) => {
					let data = res;
					// console.log(data);
					if (!data) {
						reject("Verification failed, please Login again.");
						return;
					}
					if (!data.roles || data.roles.length <= 0) {
						reject("getUserInfo: roles must be a non-null array!");
						return;
					}
					Object.assign(user.value, {...data});
					resolve(data);
				})
				.catch((error) => {
					reject(error);
				});
		});
	}

	// user logout
	function logout() {
		return new Promise<void>((resolve, reject) => {
			AuthAPI.logout()
				.then(() => {
					localStorage.setItem(TOKEN_KEY, "");
					location.reload(); // 清空路由
					resolve();
				})
				.catch((error) => {
					reject(error);
				});
		});
	}

	// remove token
	function resetToken() {
		// console.log("resetToken");
		return new Promise<void>((resolve) => {
			localStorage.setItem(TOKEN_KEY, "");
			resetRouter();
			resolve();
		});
	}

	return {
		user,
		login,
		getUserInfo,
		logout,
		resetToken,
	};
});

// 非setup
export function useUserStoreHook() {
	return useUserStore(store);
}
